package com.aiyige.page.authentication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.im.SessionHelper;
import com.aiyige.page.authentication.adapter.AuthMsgAdapter;
import com.aiyige.page.authentication.model.Authentication;
import com.aiyige.utils.MaxHeightRecyclerView;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.AuthenticationFailedPage)
public class AuthenticationFailedPage extends AppCompatActivity {
    public static final int NEXT = 1;
    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.reasonRv)
    MaxHeightRecyclerView reasonRv;
    @BindView(R.id.reauthenticationBtn)
    Button reauthenticationBtn;
    @BindView(R.id.contactCustomerServiceLayout)
    LinearLayout contactCustomerServiceLayout;
    @Autowired
    Authentication authentication = null;
    AuthMsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.authentication_failed_page);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        if (authentication == null) {
            ToastX.show(R.string.unknown_error);
            finish();
            return;
        }
        titleTv.setText(R.string.user_authentication);
        reasonRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AuthMsgAdapter();
        adapter.bindToRecyclerView(reasonRv);
        adapter.setNewData(authentication.getAuditMsg());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case NEXT:
                if (resultCode == RESULT_OK) {
                    finish();
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }

    }

    @OnClick({R.id.titleBackBtn, R.id.reauthenticationBtn, R.id.contactCustomerServiceLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
            case R.id.reauthenticationBtn:
                switch (authentication.getUserType()) {
                    case Authentication.USER_TYPE_ORGANIZATION:
                        ARouter.getInstance()
                                .build(ARouterConfig.OrganizationAuthenticationPage)
                                .withParcelable("authentication", Authentication.newBuilder()
                                        .userType(Authentication.USER_TYPE_ORGANIZATION)
                                        .readOnly(false)
                                        .build())
                                .navigation(this, NEXT);
                        break;
                    case Authentication.USER_TYPE_PERSONAL:
                        ARouter.getInstance()
                                .build(ARouterConfig.PersonalAuthenticationPage)
                                .withParcelable("authentication", Authentication.newBuilder()
                                        .userType(Authentication.USER_TYPE_PERSONAL)
                                        .readOnly(false)
                                        .build())
                                .navigation(this, NEXT);
                        break;
                }
                break;
            case R.id.contactCustomerServiceLayout:
                SessionHelper.getInstance().imService(this);
                break;
        }
    }
}
